package org.amuxia.emailtest.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.amuxia.emailtest.pojo.User;
import org.amuxia.emailtest.utils.GenerateLinkUtils;
import org.amuxia.emailtest.utils.MyJDBC;

/**
 * @author amuxia
 * 2017年7月24日
 */
@WebServlet("/ActivateServlet")
public class ActivateServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
	// TODO Auto-generated method stub
	String idValue = request.getParameter("id");
	System.out.println(idValue);
	int id = -1;
	try {
		id = Integer.parseInt(idValue);
	} catch (NumberFormatException e) {
		e.printStackTrace();
	}
	String SQL = "select * from tb_user where id=?";
	ResultSet rs= MyJDBC.query(SQL, id);
	User user = new User();
	try {
		if(rs.next()){
			user.setId(rs.getInt(1));
			user.setUsername(rs.getString(2));
			user.setPassword(rs.getString(3));
			user.setEmail(rs.getString(4));
			user.setActivated(rs.getBoolean(5));
			user.setCodeUrl(rs.getString(6));
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	//验证无误，状态更改为1，即激活
	if(GenerateLinkUtils.verifyCheckcode(user, request)){
		String updSQL = "update tb_user set activated =1 where id=?";
		MyJDBC.execute(updSQL, id);
		user.setActivated(true);
		request.getSession().setAttribute("user", user);
		request.getRequestDispatcher("/WEB-INF/jsp/pass.jsp").forward(request, response);
	}
	}
}
